package response

import (
	"fmt"
	"github.com/gogf/gf/v2/net/ghttp"
	"github.com/gogf/gf/v2/os/gview"
	"miaoyu/utility"
)

var defaultViewParams gview.Params

func init() {
	defaultViewParams = defaultParamsBuild()
}

// TplResp 通用tpl响应
type TplResp struct {
	r   *ghttp.Request
	tpl string
}

// BuildTpl 返回一个tpl响应
func BuildTpl(r *ghttp.Request, tpl string) *TplResp {
	var t = TplResp{
		r:   r,
		tpl: tpl,
	}
	return &t
}

// ErrorTpl 返回一个错误的tpl响应
func ErrorTpl(r *ghttp.Request) *TplResp {
	var t = TplResp{
		r:   r,
		tpl: "error/error.html",
	}
	return &t
}
func (resp *TplResp) WriteTplWithExit(params ...gview.Params) {
	fmt.Println("a", params)
	params = append(params, defaultViewParams)

	fmt.Println("b", params)
	resp.r.Response.WriteTpl(resp.tpl, params...)
	resp.r.Response.WriteExit()

}

// WriteTpl 输出页面模板附加自定义函数
func (resp *TplResp) WriteTpl(params ...gview.Params) {
	fmt.Println("a", params)
	for _, val := range params {
		fmt.Println("序号", val)
	}
	params = append(params, defaultViewParams)

	fmt.Println("b", params)
	resp.r.Response.WriteTpl(resp.tpl, params...)

}
func (resp *TplResp) WriteExit() {
	resp.r.Response.WriteExit()
}

func defaultParamsBuild() gview.Params {
	params := gview.Params{
		"UrlBuild": utility.Test,
	}

	return params
}
